package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.eduservice.dto.UserDto;
import com.guigu.eduservice.entity.User;
import com.guigu.eduservice.mapper.UserMapper;
import com.guigu.eduservice.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.utils.JwtUtils;
import com.guigu.eduservice.utils.MD5;
import com.guigu.eduservice.vo.UserVo;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表（平台买卖双方用户） 服务实现类
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public PageInfo<User> getUserAll(UserVo vo, Integer pageNum, Integer pageSize) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (vo.getNickname() != "")
            wrapper.like("user_name", vo.getNickname());
        if (vo.getPhone() != "")
            wrapper.eq("phone", vo.getPhone());

        PageHelper.startPage(pageNum, pageSize);
        PageInfo<User> pageInfo = new PageInfo<>(this.list(wrapper));
        return pageInfo;
    }

    @Override
    public String login(UserDto userDto) {
        //
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",userDto.getPhone());
        User one = this.getOne(queryWrapper);
        if(one == null)
            return "用户名为空";
        //判断用户输入的密码是否正确
        if(!MD5.encrypt(userDto.getPassword()).equals(one.getPassword()))
            return "密码错误";
        //生成jwt令牌：认证成功的用户对象的id, nickname
        String jwtToken = JwtUtils.getJwtToken(one.getUserId(), one.getUserName());
        return jwtToken;
    }


}
